﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yugi.Client.Card;
using Yugi.Client.Control;
using Yugi.Client;

namespace Yugi.Client
{
    public class CardChoice
    {
        public int Count { get; set; }
        public Dictionary<int,HostItem> Data { get; set; }
        public bool IsProcess { get; set; }
        public List<CardModel> ListCards { get; set; }
        public List<YBaseCard> Choices { get; set; }
        public YType SelectionType { get; set; }
        public event EventHandler OnChoiceComplete;
        public event Action<object,string> OnError;
        public CardChoice()
        {
            IsProcess = false;
            Choices = new List<YBaseCard>();
            Last = new Dictionary<int, YTrangThaiLaBai>();
            SelectionType = YType.Monster;
        }
        public void Begin()
        {
            Choices.Clear();
            Last.Clear();
            int dem = 0;
            foreach (var item in ListCards)
            {
                YCard card = Data[item.YCard.ID].CardControl;
                if(SelectionType == (YType.Spell| YType.Trap))
                {
                    if (((CardModel)card.DataContext).YCard.CardType == YType.Spell || ((CardModel)card.DataContext).YCard.CardType == YType.Trap)
                    {
                        card.ShowCanChoice();
                        card.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(card_MouseLeftButtonDown);
                        dem++;
                    }
                }
                else
                {
                    if (((CardModel)card.DataContext).YCard.CardType == SelectionType)
                    {
                        card.ShowCanChoice();
                        card.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(card_MouseLeftButtonDown);
                        dem++;
                    }
                }
            }
            if(dem<Count)
                if (OnError != null)
                {
                    OnError(this,"Trên Bàn Không Đủ Số Lượng Bài Cần Thiết!!!");
                    End();
                }
        }

        void card_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            YCard card = sender as YCard;
            YBaseCard data = ((CardModel)(card.DataContext)).YCard;
            if (Choices.Contains(data))
            {
                Choices.Remove(data);
                card.State = Last[data.ID];
            }
            else
            {
                Last[data.ID] = card.State;
                card.State = YTrangThaiLaBai.Ritual;
                Choices.Add(data);
            }
            if (Choices.Count == Count)
            {
                End();
                if (OnChoiceComplete != null)
                    OnChoiceComplete(this, new EventArgs());
               
            }
        }
        public void End()
        {
            if (ListCards == null)
                return;
            foreach (var item in ListCards)
            {
                if (!Data.ContainsKey(item.YCard.ID)) continue;
                YCard card = Data[item.YCard.ID].CardControl;
                if (card.State == YTrangThaiLaBai.Ritual)
                    card.State = Last[item.YCard.ID];
                card.HideCanChoice();
                card.MouseLeftButtonDown -= new System.Windows.Input.MouseButtonEventHandler(card_MouseLeftButtonDown);
            }
        }
        private Dictionary<int, YTrangThaiLaBai> Last;
    }
}
